package com.bible.fire.service;

import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bible.fire.api.baidumap.LocationListenerFromBaidu;
import com.bible.fire.util.SystemConstant;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

public class BDLocationService extends Service {
	
	public LocationClient mLocClient = null;
	public BDLocationListener myListener = new LocationListenerFromBaidu();
	
	@Override
	public void onCreate(){
		 super.onCreate();
		 mLocClient = new LocationClient(getApplicationContext());     //声明LocationClient类
		 mLocClient.setAK( SystemConstant.API_KEY_MAP_BAIDU );
		 mLocClient.registerLocationListener( myListener );    //注册监听函数
		 
		 LocationClientOption option = new LocationClientOption();
		 option.setOpenGps(true);
		 //option.setAddrType("all");//返回的定位结果包含地址信息
		 option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
		 option.setScanSpan(10000 * 2);//设置发起定位请求的间隔时间为20000ms
		 option.disableCache(false);//启用缓存定位
		 //option.disableCache(true);//禁用缓存定位
		 //option.setPoiNumber(5);	//最多返回POI个数	
		 //option.setPoiDistance(1000); //poi查询距离		
		 //option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息		
		 mLocClient.setLocOption(option);
		 mLocClient.start();		 
		 
		 
		 /*
		 if (mLocClient != null && mLocClient.isStarted())
				mLocClient.requestPoi();
		*/
		 /*
		 if (mLocClient != null && mLocClient.isStarted())
				mLocClient.requestOfflineLocation();
		*/
	}
	
	public int onStartCommand(Intent intent, int flags, int startId) {
		//int result = super.onStartCommand(intent, flags, startId);
		Log.d("bd service", "start to location");
		if (mLocClient != null && mLocClient.isStarted()){
			mLocClient.requestLocation();
		    //mLocClient.requestOfflineLocation();//离线定位请求
		}else{
			 Log.d("LocSDK3", "locClient is null or not started");
			 //WindowManager wm = (WindowManager)getApplicationContext().getSystemService( Context.WINDOW_SERVICE);
			 //Toast.makeText(getApplicationContext(), "locClient is null or not started", Toast.LENGTH_LONG).show();
		 }
		//return result;
		return START_STICKY;
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
